// @override
// Future<ByteData?> send(String channel, ByteData? message) {
//   final Completer<ByteData?> completer = Completer<ByteData?>();
//   // ui.PlatformDispatcher.instance is accessed directly instead of using
//   // ServicesBinding.instance.platformDispatcher because this method might be
//   // invoked before any binding is initialized. This issue was reported in
//   // #27541. It is not ideal to statically access
//   // ui.PlatformDispatcher.instance because the PlatformDispatcher may be
//   // dependency injected elsewhere with a different instance. However, static
//   // access at this location seems to be the least bad option.
//   // on that getter.
//   ui.PlatformDispatcher.instance.sendPlatformMessage(channel, message, (ByteData? reply) {
//     try {
//       completer.complete(reply);
//     } catch (exception, stack) {
//       FlutterError.reportError(FlutterErrorDetails(
//         exception: exception,
//         stack: stack,
//         library: 'services library',
//         context: ErrorDescription('during a platform message response callback'),
//       ));
//     }
//   });
//   return completer.future;
// }

import 'dart:async';

/// 从flutter与native的交互源码可以看到，completer可以帮助返回回调的结果，否则在回调函数中是无法返回当前函数的(作用域限制)。

Future<void> main() async {
  String res = await getResult();
  print(res);
}

Future<String> getResult() async {
  Completer<String> taskCompleter = Completer();
  await doTask();
  taskCompleter.complete("complete");
  return taskCompleter.future;
}

Future<void> doTask() async {
  await Future.delayed(const Duration(seconds: 3));
}

typedef TaskCallback = void Function(String result);
